
import apiFile from "@/api/apiFile";
import {createChunk} from './Chunk'

interface resultObj {
    start: number;
    end: number;
    index: number;
    hash: string;
    blob: Blob;
}
onmessage = async (e) => {
    const result = []
    let { file, start, end, chunkSize, chunks, processValue } = e.data
    for (let i = start; i < end; i++) {
        let pom = await createChunk(file, i, chunkSize) as resultObj;
        let formData = new FormData();
        formData.append("file", pom.blob);
        formData.append("chunkIndex", (pom.index).toString());
        formData.append("fileName", file.name);
        formData.append("hash", pom.hash);
        formData.append("totalChunks", chunks);
        let up_status = apiFile.fileUpload(formData);
        result.push(up_status)
    }    
    const chunksResult = await Promise.all(result)
    processValue = chunksResult.length/chunks
    postMessage(processValue)
}
